const mysql = require('mysql2/promise');

async function generateTableDefinition(connection, tableName, columns) {
    const columnDefinitions = columns.map(column => {
        return `${column.name} ${column.type}${column.nullable ? '' : ' NOT NULL'}${column.default ? ` DEFAULT ${column.default}` : ''}`;
    }).join(', ');

    const createTableQuery = `CREATE TABLE IF NOT EXISTS ${tableName} (${columnDefinitions});`;
    await connection.query(createTableQuery);
}

async function createUserTable() {
    const connection = await mysql.createConnection({
        host: 'localhost',
        user: 'root',
        database: 'your_database_name',
        password: 'your_password'
    });

    const userColumns = [
        { name: 'id', type: 'INT AUTO_INCREMENT', nullable: false },
        { name: 'username', type: 'VARCHAR(255)', nullable: false },
        { name: 'password', type: 'VARCHAR(255)', nullable: false },
        { name: 'email', type: 'VARCHAR(255)', nullable: false },
        { name: 'created_at', type: 'TIMESTAMP', nullable: false, default: 'CURRENT_TIMESTAMP' },
        { name: 'updated_at', type: 'TIMESTAMP', nullable: false, default: 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' }
    ];

    await generateTableDefinition(connection, 'users', userColumns);
    await connection.end();
}

module.exports = {
    createUserTable
};